package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.DomainService;
import com.yswy.backstage.service.UserProjectService;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.Domain;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.Softness;
import com.yswy.domain.entity.UserProject;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.DomainMapper;
import com.yswy.domain.mapper.UserProjectMapper;
import com.yswy.domain.vo.dto.BaseFilter;
import com.yswy.domain.vo.gld.addOrUpate.DomainAddOrUpdate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
@AllArgsConstructor
public class DomainServiceImpl extends ServiceImpl<DomainMapper, Domain>
        implements DomainService {

    @Override
    public Page<Domain> listDomain(BaseFilter req) {
        Page<Domain> domainPage = baseMapper.pageDomain(req
                , new Page<>(req.getPageNum() , req.getPageSize()) );
        return domainPage;
    }

    @Override
    public List<Domain> AllDomain() {
        return list(new LambdaQueryWrapper<Domain>()
                .eq(BasePojo::getIsDel , 0));
    }

    @Override
    public void addOrUpadteDomain(DomainAddOrUpdate domainAddOrUpdate) {
        User user = CurrentUserContext.getCurrentUser();
        Domain domain= null ;
        Boolean  isExist = isExist( domainAddOrUpdate.getDomainName());

        if (domainAddOrUpdate.getId() != null){
            domain = getById(domainAddOrUpdate.getId());
            Assert.state(null != domain && null != domain.getId(), NULL_MSG);
            BeanUtil.copyProperties(domainAddOrUpdate, domain);
            BasePojo.setBase(user.getId().longValue(), false, domain);
            updateById(domain);
        }else {
            if (isExist){
                throw new IllegalStateException("域名已存在");
            }
            domain = new Domain(domainAddOrUpdate , user);
            save(domain);
        }
    }

    @Override
    public Boolean isExist(String domainName) {
        Integer count = count(new LambdaQueryWrapper<Domain>()
                .eq(Domain::getDomainName , domainName)
                .eq(Domain::getIsDel , 0));
        if (count>0){
            return true ;
        }
        return false;
    }


    @Override
    public void delDomain(Long domainId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, domainId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        Domain bo = new Domain();
        bo.setId(domainId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }
}
